package com.rzico.basics.amap.util;

import com.alibaba.fastjson.JSONObject;
import com.rzico.basics.amap.pojo.GeoFencePage;
import com.rzico.basics.amap.pojo.request.ParamsRequest;
import com.rzico.basics.amap.pojo.request.SearchRequest;
import com.rzico.basics.amap.pojo.request.StatusRequest;
import com.rzico.basics.amap.pojo.response.StatusEventListResponse;
import com.rzico.basics.amap.pojo.response.StatusResponse;

/**
 * 地理围栏
 * <p>
 * Created by lam on 2018/8/8.
 */
public class GeoFenceUtil {

    // 创建围栏(POST)/查询围栏(GET)/更新围栏(POST)/删除围栏(POST)/围栏启动&停止(POST)
    private static String map_code_url = "https://restapi.amap.com/v4/geofence/meta?parameters";

    // 围栏设备监控url
    private static String map_status_url = "https://restapi.amap.com/v4/geofence/status";

    /**
     * 围栏查询
     *
     * @param request
     * @return
     */
    public static GeoFencePage searchGeoFence(SearchRequest request) {
        GeoFencePage page = null;
        try {
            String result = OKHttpUtil.httpGet(map_code_url.replace("parameters", "") + request.toString());
            JSONObject jsonObject = JSONObject.parseObject(result);
            if (jsonObject.getInteger("errcode") == 0) {
                page = JSONObject.parseObject(jsonObject.getString("data"), GeoFencePage.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return page;
    }

    /**
     * 围栏管理 【创建 - 更新 - 删除 - 启动&停止】
     */
    public static StatusResponse mangeGeoFence(ParamsRequest request) {
        StatusResponse response = null;

        if (request == null) {
            response = new StatusResponse();
            response.setError(true);
            response.setMessage("参数不能为空");
            return response;
        }

        if (request.getKey() == null) {
            response = new StatusResponse();
            response.setError(true);
            response.setMessage("无效的用户key");
            return response;
        }

        try {

            String _url = "key=" + request.getKey();

            if (request.getMethod() != null) {
                _url += "&method=" + request.getMethod();
            }

            if (request.getGid() != null) {
                _url += "&gid=" + request.getGid();
            }

            String result = OKHttpUtil.httpPost(map_code_url.replace("parameters", "") + _url, request.toString());

            JSONObject jsonObject = JSONObject.parseObject(result);
            if (jsonObject.getInteger("errcode") == 0) {
                response = JSONObject.parseObject(jsonObject.getString("data"), StatusResponse.class);
                response.setError(false);
            } else {
                response = new StatusResponse();
                response.setMessage(jsonObject.getString("errmsg"));
                response.setError(true);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
    }


    public static StatusEventListResponse statusGeoFence(StatusRequest request) {
        StatusEventListResponse response = null;

        if (request == null) {
            response = new StatusEventListResponse();
            response.setError(true);
            response.setMessage("输入的参数不能为空");
            return response;
        }

        if (request.getKey() == null || "".equals(request.getKey())) {
            response = new StatusEventListResponse();
            response.setError(true);
            response.setMessage("用户key不能为空");
            return response;
        }

        if (request.getDiu() == null || "".equals(request.getDiu())) {
            response = new StatusEventListResponse();
            response.setError(true);
            response.setMessage("用户设备标识不能为空");
            return response;
        }

        if (request.getLocations() == null || "".equals(request.getLocations())) {
            response = new StatusEventListResponse();
            response.setError(true);
            response.setMessage("设备坐标组不能为空");
            return response;
        }

        try {
            String result = OKHttpUtil.httpGet(map_status_url.replace("parameters", "") + request.toString());

            JSONObject jsonObject = JSONObject.parseObject(result);
            if (jsonObject.getInteger("errcode") == 0) {
                response = JSONObject.parseObject(jsonObject.getString("data"), StatusEventListResponse.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
    }

}
